﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace DAL
{
    public class SelectQuestionEntityDAL
    {
        private SQLHelper sqlhelper = null;

        public SelectQuestionEntityDAL()
        {
            sqlhelper = new SQLHelper();
        }

        TBToList<SelectQuestionEntity> dtToList = new TBToList<SelectQuestionEntity>();

        #region 读取全部单选题
        /// <summary>
        /// 读取全部单选题
        /// </summary>
        /// <returns>单选题集合List</returns>
        public List<SelectQuestionEntity> LoadAllSelectQuestion()
        {
            //1,查询所有的选择题
            DataTable dt = new DataTable();
            string sql = "select * from SelectQuestionEntity";

            dt = sqlhelper.ExecuteQuery(sql, CommandType.Text);

            //2,把查询结果组织成  实体List
            List<SelectQuestionEntity> lsSelectQuestionEntity = new List<SelectQuestionEntity>();
            lsSelectQuestionEntity = dtToList.ToList(dt);

            return lsSelectQuestionEntity;
        } 
        #endregion

        #region 将新的选择题信息导入到数据库-卢彦聪-2018年——陈新月-2018年5月5日17:21:25
        public int InsertNewSelectQuestion(List<SelectQuestionEntity> lstSelectQuestion)
        {
            //1,查询所有的选择题
            DataTable dt = new DataTable();
            string sqlselect = "select * from SelectQuestionEntity";
            dt = sqlhelper.ExecuteQuery(sqlselect, CommandType.Text);
            string fraction = dt.Rows[0]["Fration"].ToString();
            int i = 0;
            int j = 0;
            for (i = 0; i < lstSelectQuestion.Count; i++)
            {
                string sql = "insert into SelectQuestionEntity(QuestionID,QuestionTypeID,Fration,QuestionFlag,QuestionContent,RightAnswer,IsUse,OptionA,OptionB,OptionC,OptionD) values(@QuestionID,@QuestionTypeID,@Fration,@QuestionFlag,@QuestionContent,@RightAnswer,@IsUse,@OptionA,@OptionB,@OptionC,@OptionD)";

                lstSelectQuestion[i].QuestionID = (dt.Rows.Count + i + 1).ToString();
                SqlParameter[] paras = new SqlParameter[]{
                    new SqlParameter("@QuestionID", lstSelectQuestion[i].QuestionID),
                    new SqlParameter("@QuestionTypeID","1"),
                    new SqlParameter("@Fration", fraction),
                    new SqlParameter("@QuestionFlag","null"),
                    new SqlParameter("@QuestionContent",lstSelectQuestion[i].QuestionContent ),
                    new SqlParameter("@RightAnswer", lstSelectQuestion[i].RightAnswer),
                    new SqlParameter("@IsUse", "1"),
                    new SqlParameter("@OptionA",lstSelectQuestion[i].OptionA),
                    new SqlParameter("@OptionB",lstSelectQuestion[i].OptionB),
                    new SqlParameter("@OptionC",lstSelectQuestion[i].OptionC),
                    new SqlParameter("@OptionD",lstSelectQuestion[i].OptionD)

                };
                try
                {
                    //捕获错误数据标志             
                    int flag = sqlhelper.ExecuteNonQuery(sql, paras, CommandType.Text);
                    j++;
                }
                catch (Exception)
                {
                    //返回错误数据的标志
                    return j;
                }

            }
            return j;
        }
        #endregion

        #region 配置选择题个数，更新选择题表中每一道题的分值，默认选择题分值20分-卢彦聪-2018-4
        public int UpdateSelectQuestionFraction(SelectQuestionEntity enSelectQuestion)
         {
            int flag = 0;
            string sql = "update SelectQuestionEntity set Fration = @Fration";
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter ("@Fration",enSelectQuestion.Fration)
            };
            flag = sqlhelper.ExecuteNonQuery(sql, paras, CommandType.Text);

            return flag;

        }
        #endregion

        #region 考试模式配置，更新SetUp表-卢彦聪-2018-4 修改人：李金颖-增加is_exam属性的更新-2018年12月29日
        public int UpdateSetUp(SetUp enSetUp)
        {
            int flag = 0;
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter ("@is_showScore",enSetUp.is_showScore),
                new SqlParameter ("@is_upload",enSetUp.is_upload),
                new SqlParameter ("@is_lockScreen",enSetUp.is_lockScreen),
                new SqlParameter("@is_exam",enSetUp.is_exam),
                new SqlParameter ("@examTime",enSetUp.examTime),
                new SqlParameter ("@noticeTime",enSetUp.noticeTime),
                new SqlParameter("@screenlockkey",enSetUp.screenlockkey)
            };
            string sql = "update SetUp set is_showScore=@is_showScore,is_upload=@is_upload,is_lockScreen=@is_lockScreen,is_exam=@is_exam,examTime=@examTime,noticeTime=@noticeTime,screenlockkey=@screenlockkey";
            flag = sqlhelper.ExecuteNonQuery(sql, paras, CommandType.Text);
            return flag;
        }
        #endregion

        #region 选择题题量配置，更新SetUp表-卢彦聪-2018年4月30日
        public int UpdateSelectQuestionNum(SetUp enSetUp)
        {
            int flag = 0;
            SqlParameter[] paras = new SqlParameter[]
            { new SqlParameter ("@selectQuestionSum",enSetUp.selectQuestionSum)};
            string sql = "update SetUp set selectQuestionSum=@selectQuestionSum";
            flag = sqlhelper.ExecuteNonQuery(sql, paras, CommandType.Text);
            return flag;
        }
        #endregion

        #region 读取SetUp表中选择题数量
        public DataTable QuerySelectQuestionSum()
        {
            DataTable dt = new DataTable();
            string sql = "select selectQuestionSum from SetUp";
            dt = sqlhelper.ExecuteQuery(sql, CommandType.Text);
            return dt;
        }
        #endregion

    }
}
